#!/usr/bin/env bash

# Test successful validation
cat <<EOF >mise.toml
[tasks.valid]
run = "echo valid"

[tasks.meta-task]
depends = ["valid"]
EOF

assert_contains "mise task validate" "✓ All 2 task(s) validated successfully"

# Test circular dependency detection
cat <<EOF >mise.toml
[tasks.a]
run = "echo a"
depends = ["b"]

[tasks.b]
run = "echo b"
depends = ["a"]
EOF

assert_contains "mise task validate 2>&1 || true" "Circular dependency detected"
assert_contains "mise task validate 2>&1 || true" "circular-dependency"
assert_contains "mise task validate 2>&1 || true" "a -> b -> a"

# Test missing dependency
cat <<EOF >mise.toml
[tasks.valid]
run = "echo valid"

[tasks.invalid]
run = "echo invalid"
depends = ["non-existent"]
EOF

assert_contains "mise task validate 2>&1 || true" "Dependency 'non-existent' not found"
assert_contains "mise task validate 2>&1 || true" "missing-dependency"

# Test invalid timeout
cat <<EOF >mise.toml
[tasks.bad-timeout]
run = "echo test"
timeout = "not-a-duration"
EOF

assert_contains "mise task validate 2>&1 || true" "Invalid timeout format"
assert_contains "mise task validate 2>&1 || true" "invalid-timeout"

# Test valid timeout formats
cat <<EOF >mise.toml
[tasks.good-timeout-1]
run = "echo test"
timeout = "30s"

[tasks.good-timeout-2]
run = "echo test"
timeout = "5m"

[tasks.good-timeout-3]
run = "echo test"
timeout = "1h"
EOF

assert_contains "mise task validate" "✓ All 3 task(s) validated successfully"

# Test duplicate aliases (should only report once, not for each task)
cat <<EOF >mise.toml
[tasks.task1]
run = "echo 1"
alias = "myalias"

[tasks.task2]
run = "echo 2"
alias = "myalias"
EOF

assert_contains "mise task validate 2>&1 || true" "Alias 'myalias' is used by multiple tasks"
assert_contains "mise task validate 2>&1 || true" "alias-conflict"
# Verify the error is only reported once (not duplicated for each task)
error_count=$(mise task validate 2>&1 | grep -c "Alias 'myalias' is used by multiple tasks" || true)
[ "$error_count" = "1" ] || (echo "Expected 1 alias conflict error, got $error_count" && exit 1)

# Test empty task
cat <<EOF >mise.toml
[tasks.empty]
description = "empty task"
EOF

assert_contains "mise task validate 2>&1 || true" "Task has no executable content"
assert_contains "mise task validate 2>&1 || true" "no-execution"

# Test invalid glob pattern
cat <<EOF >mise.toml
[tasks.bad-glob]
run = "echo test"
sources = ["[invalid-glob"]
EOF

assert_contains "mise task validate 2>&1 || true" "Invalid source glob pattern"
assert_contains "mise task validate 2>&1 || true" "invalid-glob-pattern"

# Test JSON output
cat <<EOF >mise.toml
[tasks.valid]
run = "echo valid"

[tasks.invalid]
run = "echo invalid"
depends = ["missing"]
EOF

assert_contains "mise task validate --json 2>&1 || true" '"tasks_validated": 2'
assert_contains "mise task validate --json 2>&1 || true" '"errors": 1'
assert_contains "mise task validate --json 2>&1 || true" '"category": "missing-dependency"'

# Test errors-only flag
cat <<EOF >mise.toml
[tasks.error-task]
depends = ["missing"]

[tasks.warning-task]
file = "./non-executable.sh"
EOF

touch non-executable.sh
assert_contains "mise task validate --errors-only 2>&1 || true" "error-task"
assert_not_contains "mise task validate --errors-only 2>&1 || true" "warning-task"

# Test specific task validation
cat <<EOF >mise.toml
[tasks.good]
run = "echo good"

[tasks.bad]
depends = ["missing"]
EOF

assert_contains "mise task validate good" "✓ All 1 task(s) validated successfully"
assert_contains "mise task validate bad 2>&1 || true" "Dependency 'missing' not found"

# Test validation with file-based tasks
mkdir -p mise-tasks
cat <<'SCRIPT' >mise-tasks/filetask.sh
#!/usr/bin/env bash
#MISE description="Test file task"
#MISE depends=["valid"]
echo "file task"
SCRIPT
chmod +x mise-tasks/filetask.sh

cat <<EOF >mise.toml
[tasks.valid]
run = "echo valid"
EOF

assert_contains "mise task validate filetask" "✓ All 1 task(s) validated successfully"

# Test missing file
cat <<EOF >mise.toml
[tasks.missing-file]
file = "./does-not-exist.sh"
EOF

assert_contains "mise task validate 2>&1 || true" "Task file not found"
assert_contains "mise task validate 2>&1 || true" "missing-file"

# Clean up before next test
rm -rf mise-tasks non-executable.sh

# Test that run entries can reference tasks by alias (not just by name)
cat <<'EOF' >mise.toml
[tasks.build]
run = "echo building"
alias = "b"

[tasks.deploy]
run = [
  { task = "b" }
]
EOF

assert_contains "mise task validate" "✓ All 2 task(s) validated successfully"

# Test that command-line validation can use task aliases
assert_contains "mise task validate b" "✓ All 1 task(s) validated successfully"
assert_contains "mise task validate build" "✓ All 1 task(s) validated successfully"
